﻿#include "additem.h"
#include "ui_additem.h"

AddItem::AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddItem)
{
    ui->setupUi(this);
}

AddItem::~AddItem()
{
    delete ui;
}

void AddItem::on_btn_submit_clicked()
{
    QString pub_id=ui->pub_id->text().trimmed();
    QString label=ui->label->text().trimmed();
    if(pub_id.isEmpty() || label.isEmpty())
        return;

    QString pub_date=ui->pub_date->text().trimmed();
    QString applicant=ui->applicant->text().trimmed();

    if(addItem(pub_id,pub_date,applicant,label))
        accept();
    else
        QMessageBox::warning(this,QStringLiteral("操作失败"),
                             QStringLiteral("异常情况，记录无法提交！"));
}

bool AddItem::addItem(const QString &pub_id, const QString &pub_date,
                      const QString &applicant, const QString &label)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 values (?,?,?,?)")
                  .arg(ST_PATENT));
    query.addBindValue(pub_id);
    query.addBindValue(pub_date);
    query.addBindValue(applicant);
    query.addBindValue(label);
    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}
